#! /bin/sh -e

echo "setting environment config"
echo "ARTEMIS_WS_URL=$ARTEMIS_WS_URL"
echo "ARTEMIS_URL=$ARTEMIS_URL"
echo "AUTH_URL=$AUTH_URL"
echo "BRMS_URL=$BRMS_URL"
echo "SERVER_NAME=$SERVER_NAME"
echo "PUSH_URL=$PUSH_URL"
echo "BUDGET_URL=$BUDGET_URL"
echo "CTRIP_URL=$CTRIP_URL"
echo "VND_INFO_URL=$VND_INFO_URL"
echo "VND_DATA_URL=$VND_DATA_URL"
echo "API_URL=$API_URL"

cat >> /etc/nginx/conf.d/hly-admin.conf <<EOF
server {
    listen      80;
    server_name   $SERVER_NAME;

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;
    proxy_send_timeout 300s;

    gzip on;
    gzip_static on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 5;
    gzip_types text/plain application/javascript application/x-javascript text/css text/javascript;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    location / {
        try_files \$uri /index.html;
        root /app/www/;
    }

    location /oauth/ {
        proxy_pass $AUTH_URL/auth/oauth/;
    }

    location /multilanguage/ {
        proxy_pass https://cloudhelios-static.oss-cn-shanghai.aliyuncs.com/multilanguage/;
        proxy_set_header Host "cloudhelios-static.oss-cn-shanghai.aliyuncs.com";
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Credentials true;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    }

    location /api/ {
        proxy_pass $ARTEMIS_URL/api/;
        client_max_body_size 20m;
    }

    location /operationservice/ {
        proxy_pass $API_URL/operationservice/;
    }

    location /push/ {
        proxy_pass $PUSH_URL/push/;
    }

    location /budget-service/  {
        proxy_pass $BUDGET_URL/budget-service/;
        client_max_body_size 20m;
    }

    location /receipt/  {
        proxy_pass $RECEIPT_URL/receipt/;
    }

    location /vendor-data-service/ {
        proxy_pass $VND_DATA_URL/vendor-data-service/;
    }

    location /vendor-info-service/ {
        proxy_pass $VND_INFO_URL/vendor-info-service/;
    }

    location /location-service/ {
        proxy_pass $VND_DATA_URL/location-service/;
    }

    location /ctrip/ {
        proxy_pass $CTRIP_URL/ctrip/;
    }

    location /brms/ {
          proxy_pass $BRMS_URL/brms/;
    }

     location /open/ {
          proxy_pass $OPEN_URL/open/;
    }

    location /ws/direction/ {
        proxy_pass $MAP_URL/ws/direction/;
    }

    location /ws/place/ {
        proxy_pass $MAP_URL/ws/place/;
    }

    location /amap/{
        proxy_pass http://restapi.amap.com/;
        proxy_redirect off;
        proxy_set_header Host restapi.amap.com;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 4k;
        proxy_buffers 6 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        root index.php;

    }


}
EOF
cat /etc/nginx/conf.d/hly-admin.conf
echo "starting web server"

nginx -g 'daemon off;'



